/***************************************************************************************
****************************************************************************************
* FILE		: bt.c
* Description	: 
*			  
* Copyright (c) 2020 by Simlife. All Rights Reserved.
* 
* History:
* Version		Name		Date				Description
   0.1		Alen			2020/10/26		Initial Version
   
****************************************************************************************
****************************************************************************************/

#include "includes.h"
#include <string.h>

void initWifiControlPin(void)
{
   wifiControlTypeControl &= wifiControlPinReverse;   //output
   wifiControlPullUpControl &= wifiControlPinReverse; //no pull up
   wifi_OFF();
}

void closeWifiControlPin(void)
{
   wifi_OFF();
}

union
{
   unsigned char usr;
   struct
   {
      unsigned char __txif : 1;
      unsigned char __tidle : 1;
      unsigned char __rxif : 1;
      unsigned char __ridle : 1;
      unsigned char __oerr : 1;
      unsigned char __ferr : 1;
      unsigned char __nf : 1;
      unsigned char __perr : 1;
   } detail;
} unionBTFlag;

void closeUARTPin(void)
{
   _pdc1 = 0;
   _pdpu1 = 0;
   _pd1 = 0;
   _pdc2 = 0;
   _pdpu2 = 0;
   _pd2 = 0;
}

void uart_sendbyte(U8 byte)
{
   _txr_rxr = byte;
   while (!_txif)
      ;
   unionBTFlag.usr = _usr;
}

void uart_sendbuf(U8 *buf, U8 number)
{
   U8 i;

   for (i = 0; i < number; i++)
   {
      // uart_sendbyte(*buf);
      // buf++;
      _txr_rxr = buf[i];
      while (!_txif)
         ;
      unionBTFlag.usr = _usr;
   }
}

void sendTestBuf(void)
{
   U8 buf[8] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88};
   uart_sendbuf(buf, 8);
}

void sendModulePowerOff(void)
{
   U8 buf[8] = {0xFC, 0x04, 0x00, 0x00, 0x00, 0x00, 0x04, 0xCF};
   uart_sendbuf(buf, 8);
}

U8 checksum(U8 *buf, U8 lenth)
{
   U8 cnt, chekssum;

   chekssum = 0x00;

   for (cnt = 1; cnt < lenth; cnt++)
   {
      chekssum += buf[cnt];
   }
   //chekssum = (~chekssum) & 0xff;

   return chekssum;
}

void push_data_in_send_fifo(U8 *data_buf, U8 data_len)
{
   memcpy((U8 *)stSentFifo.send_buff_arrary[stSentFifo.input].send_data, data_buf, data_len);
   stSentFifo.send_buff_arrary[stSentFifo.input].send_data_len = data_len;
   stSentFifo.input++;
   if (stSentFifo.input >= SEND_BUFFER_MAX)
   {
      stSentFifo.input = 0;
   }
}

void send_command_to_bt(BT_COMMAND bt_command, U8 *body_buf)
{
   U8 bt_pro_buff[8] = {0};
   U8 pack_cnt = 0, ii = 0, chsum = 0;

   bt_pro_buff[pack_cnt++] = 0xFC;
   bt_pro_buff[pack_cnt++] = bt_command;

   for (ii = 0; ii < 4; ii++)
   {
      bt_pro_buff[pack_cnt++] = body_buf[ii];
   }
   //ii += 3;
   chsum = checksum(bt_pro_buff, pack_cnt);
   //pack[3+t_pro.len] = chsum;
   bt_pro_buff[pack_cnt++] = chsum;
   bt_pro_buff[pack_cnt++] = 0xCF;

   push_data_in_send_fifo(bt_pro_buff, 8);
}

void bt_data_send_automatic(void)
{
   U8 ii;

   if (stSentFifo.output == stSentFifo.input)
   {
      return;
   }

   for (ii = 0; ii < 8; ii++)
   {
      uart_sendbyte(stSentFifo.send_buff_arrary[stSentFifo.output].send_data[ii]);
   }

   for (ii = 0; ii < 8; ii++)
   {
      stSentFifo.send_buff_arrary[stSentFifo.output].send_data[ii] = 0;
   }
   stSentFifo.send_buff_arrary[stSentFifo.output].send_data_len = 0;
   stSentFifo.output++;
   if (stSentFifo.output >= SEND_BUFFER_MAX)
   {
      stSentFifo.output = 0;
   }
}

void bt_receive_data_process(void)
{
   U8 cksum, command, receive_pack_len;
   U8 ii;

   if ((stReceiveFifo.output == stReceiveFifo.input) || (!flagSystemStatus))
      return;

   receive_pack_len = stReceiveFifo.receive_buff_arrary[stReceiveFifo.output].receive_data_len;
   if (receive_pack_len != 0)
   {
      cksum = checksum((U8 *)stReceiveFifo.receive_buff_arrary[stReceiveFifo.output].receive_data, receive_pack_len - 2);
      if (cksum == stReceiveFifo.receive_buff_arrary[stReceiveFifo.output].receive_data[receive_pack_len - 2])
      {
         command = stReceiveFifo.receive_buff_arrary[stReceiveFifo.output].receive_data[1];
         switch (command)
         {
         case KEY_COMMAND:
         case CHECK_COMMAND:
         case OFF_COMMAND:
            break;

         case MODE_COMMAND:
            modeChange();
            break;
         default:
            break;
         }
      }
   }
   stReceiveFifo.receive_buff_arrary[stReceiveFifo.output].receive_data_len = 0;
   for (ii = 0; ii < RECEIVE_BUFFER_MAX; ii++)
   {
      stReceiveFifo.receive_buff_arrary[stReceiveFifo.output].receive_data[ii] = 0;
   }
   stReceiveFifo.output++;
   if (stReceiveFifo.output >= RECEIVE_BUFFER_MAX)
   {
      stReceiveFifo.output = 0;
   }
}
